package data_propagate.meta;

import data_propagate.meta.exception.DataPropagateMetaManagerException;
import drds.data_propagate.common.LifeCycle;
import drds.propagate.protocol.ClientId;
import drds.propagate.protocol.position.Position;
import drds.propagate.protocol.position.PositionRange;

import java.util.List;
import java.util.Map;

/**
 * meta信息管理器
 *
 * @author jianghang 2012-6-14 下午09:28:48
 * @author zebin.xuzb
 * @version 1.0.0
 */
public interface MetaManager extends LifeCycle {

    /**
     * 增加一个 client订阅 <br/>
     * 如果 client已经存在，则不做任何修改
     */
    void subscribe(ClientId clientId) throws DataPropagateMetaManagerException;

    /**
     * 判断是否订阅
     */
    boolean hasSubscribe(ClientId clientId) throws DataPropagateMetaManagerException;

    /**
     * 取消client订阅
     */
    void unsubscribe(ClientId clientId) throws DataPropagateMetaManagerException;

    /**
     * 获取 cursor 游标
     */
    Position getCursor(ClientId clientId) throws DataPropagateMetaManagerException;

    /**
     * 更新 cursor 游标
     */
    void updateCursor(ClientId clientId, Position position) throws DataPropagateMetaManagerException;

    /**
     * 根据指定的destination列出当前所有的clientIdentity信息
     */
    List<ClientId> listAllSubscribeInfo(String destination) throws DataPropagateMetaManagerException;

    /**
     * 获得该client最新的一个位置
     */
    PositionRange getFirstBatch(ClientId clientId) throws DataPropagateMetaManagerException;

    /**
     * 获得该clientId最新的一个位置
     */
    PositionRange getLastestBatch(ClientId clientId) throws DataPropagateMetaManagerException;

    /**
     * 为 client 产生一个唯一、递增的id
     */
    Long addBatch(ClientId clientId, PositionRange positionRange) throws DataPropagateMetaManagerException;

    /**
     * 指定batchId，插入batch数据
     */
    void addBatch(ClientId clientId, PositionRange positionRange, Long batchId)
            throws DataPropagateMetaManagerException;

    /**
     * 根据唯一messageId，查找对应的数据起始信息
     */
    PositionRange getBatch(ClientId clientId, Long batchId) throws DataPropagateMetaManagerException;

    /**
     * 对一个batch的确认
     */
    PositionRange removeBatch(ClientId clientId, Long batchId) throws DataPropagateMetaManagerException;

    /**
     * 查询当前的所有batch信息
     */
    Map<Long, PositionRange> listAllBatchs(ClientId clientId) throws DataPropagateMetaManagerException;

    /**
     * 清除对应的batch信息
     */
    void clearAllBatchs(ClientId clientId) throws DataPropagateMetaManagerException;

}
